
define(function(require,exports,module) { 
    // require('./plug/hammer.min'); 
	// require('./plug/lazyload_echo');
	// require('./shop');
    var myPub = require('./public'); 
    
    var httpImageUrl;
    var oHeader = $('.js_header');              	//头部
    var oFooter = $('.js_footer');              	//底部 
    var oContain = $('.js_contain');				//主体
    var oChangListBtn = $('.js_changListBtn');				//改变产品展示按钮
    var oGoods_showChangeBox = $('.js_goods_showChangeBox');	//搜索展示的产品盒子
    var oChangePriceBtn = $('.js_changePrice'); 		//价格按钮
    var oChangeSortDirBtn = $('.js_changeSortDir'); 		//综合排序按钮
    var oSearchNav = $('.js_searchNav');				//搜索列表导航
    var sortBy = "order_list";								//排序
    var page = 1 ;
	oHeader.on("touchmove",function(e) {e.preventDefault()});
    oFooter.on("touchmove",function(e) {e.preventDefault()});
    //oSearchBox.on("touchmove",function(e) {e.preventDefault()});
    
    //产品懒加载  异步加载 调用这个
    myPub.lazLoadingImgEcho()
    
    //导航增加定位
    
    var $window = $(window);
    addFixed();
	$window.on('scroll',function(e) { 
		myPub.throttle(addFixed,window)
	});
	
	
    
    //改变产品展示方式
    oChangListBtn.hammer().on('tap',function() {
    	if($(this).find('span').hasClass('icon-search_card')) {
    		$(this).find('span').removeClass('icon-search_card').addClass('icon-search_list')
    		oGoods_showChangeBox.removeClass('active');
    		oGoods_showChangeBox.find('li figure').removeAttr('style')
    	}
    	else {
    		$(this).find('span').addClass('icon-search_card').removeClass('icon-search_list')
    		oGoods_showChangeBox.addClass('active');
    		
    		myPub.SetImgHeight(oGoods_showChangeBox);
    	}
    	
    });
    //默认排序
    var sortDir = "desc";
	
    //价格改变
    oChangePriceBtn.hammer().on('tap',function() {
    	if($(this).hasClass('on')) {
    		//高到低
    		$(this).removeClass('on');
    		removeNode();
    		sortDir = "asc";
    		
    	}
    	else {
    		//低到高
    		$(this).addClass('on');
    		removeNode();
    		sortDir = "desc";
    	}
    	 
    })
    //改变综合排序
    oChangeSortDirBtn.hammer().on('tap',function() {
    	if($(this).hasClass('on')) {
    		//高到低
    		$(this).removeClass('on');
    		removeNode();
    		sortDir = "desc";
    		
    	}
    	else {
    		//低到高
    		$(this).addClass('on');
    		removeNode();
    		sortDir = "asc";
    	}
    	
    })
    
    
    //导航点击
    oSearchNav.find('li').hammer().on('tap',function() {
    	var index = $(this).index();
    	if(index == 0){
    		sortBy = "order_list";
    	}else if(index == 1){
    		sortBy = "sales_count";
    	}else if(index == 2){
    		sortBy = "sale_price";
    		removeNode();
    	}
    	
    	//第四个li是展示方式改变
    	if(index !== 3) {
    		oSearchNav.find('li').removeClass('active').eq(index).addClass('active');
    		removeNode();
    		start = 1 ;//切换导航初始化start
    		loadData();
    	}
    })
    
	module.exports = {
        init:function(params1,params2){   
        	httpImageUrl = params1;
        	mshopdomain =  params2;
        	loadData();
        }
    };
    
    function addFixed() {
    	$window.scrollTop() > 80 ? oSearchNav.addClass('fixedDom') : oSearchNav.removeClass('fixedDom')
    }
	
	
	//显示更多
	var  oMoreBtn = $('.js_moreBtn'); 			//加载更多按钮
	var  start = 1 ;
	oMoreBtn.hammer().on('tap',function() {
		var _this = $(this);
		_this.hide().siblings('.js_loadingBtn').show().find('.icoStyle').addClass('myRotateRefresh');
		
		//显示更多 
		++start;
		loadData();
		
		myPub.HamstopPropaga();
	})
	
	
	var keyword = $("#keyword").val();
	var limit = 12 ;
	var tmp = '';
	var imageUrl = "";
	//异步加载ajax请求  根据导航排序所有
	function loadData(){
        var url = "/shop/goods/searchv4/"+sortBy;
        var html = '';
        $.ajax({
            url:url,
            type:"post",
            data:{sortDir:sortDir,start:start,limit:limit,keyword:keyword},
			dataType:"json",
			success:function(data){
				if(data!=null){
					var goodsList = eval(data);
					if(goodsList.length == 0){
						$(".js_loadingBtn").hide();
						$(".js_noMoreBtn").show();
						$(".js_moreBtn").hide();
						$(".errPage").hide();
					}else{
						//$('#listAppend_'+(page)+'').remove();
						var m = '';
						var t = null;
						 
						$.each(goodsList,function(index,obj){
							
							t = viewList(obj);
							m = m + t;
						});
						html = html + m;
						$('#listAppend_'+(page-1)+'').append(html); 
						
						
						
						if(goodsList.length<limit){
							$(".js_loadingBtn").hide();
							$(".js_noMoreBtn").show();
							$(".js_moreBtn").hide();
							$(".errPage").hide();
						}else{
							$(".js_loadingBtn").hide();
							$(".js_noMoreBtn").hide();
							$(".js_moreBtn").show();
							$(".errPage").hide();
						}
						
					}
				}else{
					$(".js_loadingBtn").hide();
					$(".js_noMoreBtn").hide();
					$(".js_moreBtn").hide();
					$(".errPage").show();
				}
				
				
			},
			complete:function(XHR) { 
				myPub.lazLoadingImgEcho()
				XHR = null;
				
			}
		});
	}

	//  'src="'+obj.goodsLabels+'"  
	//查询的list进行拼接
	function viewList(obj){
		//<#if isstatic=="true">${mshopdomain}/shop/goods/html/${obj.goodsSn}<#else>/shop/goods/view/${obj.goodsSn}</#if>
		//先拼接价格字符串，团购商品需同时显示团购价
		var priceStr = "";
		if(obj.isTuangou == true && typeof(obj.tuangouPrices)!="undefined"){
            priceStr = '<i>￥</i><span class="goodsPrice">'+obj.tuangouPrices+'</span><span class="goodsWeight">/</span>' + '<span class="goodsWeight del Bflex1" style="margin-left: 0;">￥'+obj.salePrice+'</span>';
		}else{
            priceStr = '<i>￥</i><span class="goodsPrice">'+obj.salePrice+'</span><span class="goodsWeight Bflex1">/'+obj.productName+'</span>';
		}
		var temp;
		if(typeof(obj.rfName)!="undefined"){
			tmp = '<li ><a class="BoxSelf js_BoxSelf" href="/shop/goods/view/'+obj.goodsSn+'" onclick="GIOSearchDataClick('+'\''+keyword+'\''+','+obj.goodsCategoryId+','+obj.id+','+'\''+obj.name+'\''+','+'\''+obj.goodsSn+'\''+')"><figure>'
				+'<img class="js_lazyImg" data-echo="'+httpImageUrl+obj.showImage+ smallImgArg+'"'
				+ 'src="/static_v3/image/loadImg.jpg">'
				+'<i class="tip-city">'+obj.rfName+'</i>'
				+'<i class="tip-th" style="display:none"></i></figure><aside class="priceArea VBox Bflex1"><div class="Bflex1 priceArea_name">'
				+'<h4>';
            if(obj.goodsLabels && obj.goodsLabels != ''){
                tmp += '<span class="promotionHotIcon">'+obj.goodsLabel+'</span>'
            }
            tmp += obj.name+'</h4><p class="smallFont_tip">'+getTitle(obj.title)+'</p></div><div class="priceArea_price"><p class="HBox BoxCenter priceAreaBox">'
				+ priceStr
				+'</p></div><p class="addCar js_addCar" pId="'+obj.productId+'"><em class="icon-shopcar_small"></em></p></aside></a>';
            if(obj.label && obj.label != ''){
                tmp += '<div class="promotionSale horpromotionSale">';
                for(var i=0;i<obj.labels.length;i++){
                    tmp += '<span>'+obj.labels[i]+'</span>'
                }
                tmp += '</div>';
            }
            tmp += '</li>';
		}else{
			tmp = '<li ><a class="BoxSelf js_BoxSelf" href="/shop/goods/view/'+obj.goodsSn+'" onclick="GIOSearchDataClick('+'\''+keyword+'\''+','+obj.goodsCategoryId+','+obj.id+','+'\''+obj.name+'\''+','+'\''+obj.goodsSn+'\''+')"><figure>'
				+'<img class="js_lazyImg" data-echo="'+httpImageUrl+obj.showImage+ smallImgArg+'"'
				+ 'src="/static_v3/image/loadImg.jpg">'
				+'<i class="tip-th" style="display:none"></i></figure><aside class="priceArea VBox Bflex1"><div class="Bflex1 priceArea_name">'
				+'<h4>';
			if(obj.goodsLabels && obj.goodsLabels != ''){
                tmp += '<span class="promotionHotIcon">'+obj.goodsLabel+'</span>'
            }
			tmp += obj.name+'</h4><p class="smallFont_tip">'+getTitle(obj.title)+'</p></div><div class="priceArea_price"><p class="HBox BoxCenter priceAreaBox">'
                + priceStr+'</p></div>' +
                '   <p class="addCar js_addCar" pId="'+obj.productId+'"><em class="icon-shopcar_small"></em></p>' +
                '    </aside>' +
                '   </a> ' ;
            if(obj.label && obj.label != ''){
                tmp += '<div class="promotionSale horpromotionSale">';
                for(var i=0;i<obj.labels.length;i++){
                    tmp += '<span>'+obj.labels[i]+'</span>'
                }
                tmp += '</div>';
            }
            tmp += '</li>';
		}

		return tmp;
	}
	function getTitle(obj){
		if(obj == null){
			return null;
		}else if(obj == 'undefined'){
			return null;
		}else{
			return obj;
		}
		
	}
	//切换导航时去除所有的html
	function removeNode(){
			$("#listAppend_0").find('li').remove();
	}

});